package com.zj.sy.service.impl;

import cn.hutool.core.thread.ThreadUtil;
import com.alibaba.fastjson.JSONObject;
import com.zj.sy.mapper.DotDataMapper;
import com.zj.sy.service.DotDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;

import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;

@Service
public class DotDataServiceImpl implements DotDataService {
    @Autowired
    private DotDataMapper dotDataMapper;

    @Override
    public Object getDataDot() {
        LocalDateTime endDate = LocalDateTime.now();
        LocalDateTime startDate = endDate.minusDays(180);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ArrayList<Object> data = new ArrayList<>();

        CountDownLatch countDownLatch = new CountDownLatch(2);

        ThreadUtil.execAsync(new Runnable() {
            @Override
            public void run() {



        try {
            b(endDate, startDate, sdf, data);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        try {
            c(endDate, startDate, sdf, data);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        try {
            d(endDate, startDate, sdf, data);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        try {
            e(endDate, startDate, sdf, data);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        try {
            f(endDate, startDate, sdf, data);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        try {
            g(endDate, startDate, sdf, data);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        try {
            h(endDate, startDate, sdf, data);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        try {
            i(endDate, startDate, sdf, data);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

                countDownLatch.countDown();

            }
        });


        ThreadUtil.execAsync(new Runnable() {
            @Override
            public void run() {



        try {
            j(endDate, startDate, sdf, data);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        try {
            k(endDate, startDate, sdf, data);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        try {
            l(endDate, startDate, sdf, data);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        try {
            m(endDate, startDate, sdf, data);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        try {
            n(endDate, startDate, sdf, data);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        try {
            o(endDate, startDate, sdf, data);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        try {
            p(endDate, startDate, sdf, data);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

                countDownLatch.countDown();

            }
        });

        try {
            countDownLatch.await();
            ArrayList<Object> list = new ArrayList<>();
            data.forEach(x->{
                if (x instanceof JSONObject) {
                    JSONObject o = (JSONObject) x;
                    if (null != o.get("shape")) {
                        list.add(x);

                    }
                }
            });


            return list;
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public Object getById(Long id, String type) {
        JSONObject jb = dotDataMapper.getWgyById(id);
//        if(id!=null && type!=null && !type.isEmpty()){
//            if(
//                    type.equals("公共安全巡查-安全问题上报")||
//                    type.equals("社会治安巡查-治安问题上报")||
//                    type.equals("社会稳定巡查-稳定问题上报")||
//                    type.equals("社会稳定巡查-信访问题上报")||
//                    type.equals("公共安全巡查-交通隐患上报")||
//                    type.equals("公共安全巡查-消防隐患上报")||
//                    type.equals("公共安全巡查-装修安全上报")||
//
//                    type.equals("装修安全")||
//                    type.equals("安全问题")||
//                    type.equals("治安问题")||
//                    type.equals("稳定问题")||
//                    type.equals("信访问题")||
//                    type.equals("交通隐患")||
//                    type.equals("消防安全")
//            ){
//                jb = dotDataMapper.getXzGgaqxcjlById(id);
//            }else if(
//                    type.equals("社会治安巡查-重点人员走访")
//            ){
//                jb = dotDataMapper.getXzZdryxcjlById(id);
//            }else if(
//                    type.equals("社会治安巡查-重点场所巡查")
//            ){
//                jb = dotDataMapper.getXzZdcsxcjlById(id);
//            }else if(
//                    type.equals("社会治安巡查-出租群租房走访")
//            ){
//                jb = dotDataMapper.getXzCzqzfxcjlById(id);
//            }else if(
//                    type.equals("社会治安巡查-网络线索上报")
//            ){
//                jb = dotDataMapper.getXzSbxsjlById(id);
//            }else if(
//                    type.equals("矛盾纠纷巡查-矛盾纠纷")
//            ){
//                jb = dotDataMapper.getXzMdjfjlById(id);
//            }else if(
//                    type.equals("公共安全巡查-生产隐患上报") ||
//                    type.equals("社会治安巡查-交通隐患上报") ||
//                    type.equals("社会稳定巡查-消防隐患上报") ||
//                    type.equals("社会稳定巡查-食药卫隐患上报") ||
//                    type.equals("社会稳定巡查-地质隐患上报") ||
//
//                    type.equals("生产隐患") ||
//                    type.equals("消防隐患") ||
//                    type.equals("食药卫生隐患") ||
//                    type.equals("地质隐患") ||
//                    type.equals("校园安全")
//                    //application_properties表中 property_code （MM00001，MM00003）都有装修安全，测试数据库
////                            ||type.equals("装修安全")
//            ){
//                jb = dotDataMapper.getXzShwdxcjlById(id);
//            }else if(
//                    type.equals("民生服务巡查-空巢老人巡查")
//            ){
//                jb = dotDataMapper.getXzKclrxcbById(id);
//            }else if(
//                    type.equals("民生服务巡查-社情民意巡查")
//            ){
//                jb = dotDataMapper.getXzSqmyjlbById(id);
//            }else if(
//                    type.equals("民生服务巡查-政策法规宣传")
//            ){
//                jb = dotDataMapper.getXzZcfgxchdjlById(id);
//            }else if(
//                    type.equals("民生服务巡查-特殊人群帮扶")
//            ){
//                jb = dotDataMapper.getXzBfhdjllById(id);
//            }else if(
//                    type.equals("城市管理巡查-市容问题上报") ||
//                    type.equals("城市管理巡查-国土问题上报") ||
//                    type.equals("城市管理巡查-林渔问题上报") ||
//                    type.equals("城市管理巡查-宗教问题上报") ||
//                    type.equals("城市管理巡查-气象隐患上报") ||
//
//                    type.equals("市容问题") ||
//                    type.equals("国土问题") ||
//                    type.equals("林渔问题") ||
//                    type.equals("宗教问题") ||
//                    type.equals("气象问题")
//            ){
//                jb = dotDataMapper.getXzCsglxcjlById(id);
//            }else if(
//                    type.equals("城市管理巡查-环境问题上报")
//            ){
//                jb = dotDataMapper.getXzHbxcjlById(id);
//            }else if(
//                    type.equals("专项问题巡查-重点地区巡查")
//            ){
//                jb = dotDataMapper.getSzZdqyxcById(id);
//            }else if(
//                    type.equals("专项问题巡查-顽瘴痼疾巡查")
//            ){
//                jb = dotDataMapper.getXzWzgjbById(id);
//            }else if(
//                    type.equals("专项问题巡查-农机问题处理")
//            ){
//                jb = dotDataMapper.getXzNjwtsbbById(id);
//            }
//        }



        return jb;
    }

    private void p(LocalDateTime endDate, LocalDateTime startDate, SimpleDateFormat sdf, ArrayList<Object> data) {
        ArrayList<JSONObject> p = dotDataMapper.getXzNjwtsbb(startDate, endDate);
        p.forEach(x->{
            JSONObject jb = new JSONObject();
            jb.put("id", x.get("id"));
            jb.put("shape", x.get("dian"));
            JSONObject jb2 = new JSONObject();
            jb2.put("地址", "");
            jb2.put("上报时间", sdf.format(x.get("create_time")));
            jb2.put("事件类型", "专项问题巡查-农机问题处理");
            jb.put("attribute", jb2);
            data.add(jb);
        });
    }

    private void o(LocalDateTime endDate, LocalDateTime startDate, SimpleDateFormat sdf, ArrayList<Object> data) {
        ArrayList<JSONObject> o = dotDataMapper.getXzWzgjb(startDate, endDate);
        o.forEach(x->{
            JSONObject jb = new JSONObject();
            jb.put("id", x.get("id"));
            jb.put("shape", x.get("dian"));
            JSONObject jb2 = new JSONObject();
            jb2.put("地址", "");
            jb2.put("上报时间", sdf.format(x.get("create_time")));
            jb2.put("事件类型", "专项问题巡查-顽瘴痼疾巡查");
            jb.put("attribute", jb2);
            data.add(jb);
        });
    }

    private void n(LocalDateTime endDate, LocalDateTime startDate, SimpleDateFormat sdf, ArrayList<Object> data) {
        ArrayList<JSONObject> n = dotDataMapper.getSzZdqyxc(startDate, endDate);
        n.forEach(x->{
            JSONObject jb = new JSONObject();
            jb.put("id", x.get("id"));
            jb.put("shape", x.get("dian"));
            JSONObject jb2 = new JSONObject();
            jb2.put("地址", "");
            jb2.put("上报时间", sdf.format(x.get("create_time")));
            jb2.put("事件类型", "专项问题巡查-重点地区巡查");
            jb.put("attribute", jb2);
            data.add(jb);
        });
    }

    private void m(LocalDateTime endDate, LocalDateTime startDate, SimpleDateFormat sdf, ArrayList<Object> data) {
        ArrayList<JSONObject> m = dotDataMapper.getXzHbxcjl(startDate, endDate);
        m.forEach(x->{
            JSONObject jb = new JSONObject();
            jb.put("id", x.get("id"));
            jb.put("shape", x.get("dian"));
            JSONObject jb2 = new JSONObject();
            jb2.put("地址", x.get("qhnxxdz"));
            jb2.put("上报时间", sdf.format(x.get("create_time")));
            jb2.put("事件类型", "城市管理巡查-环境问题上报");
            jb.put("attribute", jb2);
            data.add(jb);
        });
    }

    private void l(LocalDateTime endDate, LocalDateTime startDate, SimpleDateFormat sdf, ArrayList<Object> data) {
        ArrayList<JSONObject> l = dotDataMapper.getXzCsglxcjl(startDate, endDate);
        l.forEach(x-> {
                    JSONObject jb = new JSONObject();
                    jb.put("id", x.get("id"));
                    jb.put("shape", x.get("dian"));

                    JSONObject jb2 = new JSONObject();
                    jb2.put("地址", x.get("qhnxxdz"));

                    jb2.put("上报时间", sdf.format(x.get("create_time")));

                    if (x.get("xslxdm").equals("10")) {

                        jb2.put("事件类型", "城市管理巡查-市容问题上报");

                    } else if (x.get("xslxdm").equals("20")) {

                        jb2.put("事件类型", "城市管理巡查-国土问题上报");

                    } else if (x.get("xslxdm").equals("30")) {

                        jb2.put("事件类型", "城市管理巡查-林渔问题上报");

                    } else if (x.get("xslxdm").equals("40")) {

                        jb2.put("事件类型", "城市管理巡查-宗教问题上报");

                    } else if (x.get("xslxdm").equals("50")) {

                        jb2.put("事件类型", "城市管理巡查-气象隐患上报");
                    }
                    jb.put("attribute", jb2);

                    data.add(jb);
                }
            );
    }

    private void k(LocalDateTime endDate, LocalDateTime startDate, SimpleDateFormat sdf, ArrayList<Object> data) {
        ArrayList<JSONObject> k = dotDataMapper.getXzBfhdjll(startDate, endDate);
        k.forEach(x->{
            JSONObject jb = new JSONObject();
            jb.put("id", x.get("id"));
            jb.put("shape", x.get("dian"));
            JSONObject jb2 = new JSONObject();
            jb2.put("地址", "");
            jb2.put("上报时间", sdf.format(x.get("create_time")));
            jb2.put("事件类型", "民生服务巡查-特殊人群帮扶");
            jb.put("attribute", jb2);
            data.add(jb);
        });
    }

    private void j(LocalDateTime endDate, LocalDateTime startDate, SimpleDateFormat sdf, ArrayList<Object> data) {
        ArrayList<JSONObject> j = dotDataMapper.getXzZcfgxchdjl(startDate, endDate);
        j.forEach(x->{
            JSONObject jb = new JSONObject();
            jb.put("id", x.get("id"));
            jb.put("shape", x.get("dian"));
            JSONObject jb2 = new JSONObject();
            jb2.put("地址", "");
            jb2.put("上报时间", sdf.format(x.get("create_time")));
            jb2.put("事件类型", "民生服务巡查-政策法规宣传");
            jb.put("attribute", jb2);
            data.add(jb);
        });
    }

    private void i(LocalDateTime endDate, LocalDateTime startDate, SimpleDateFormat sdf, ArrayList<Object> data) {
        ArrayList<JSONObject> i = dotDataMapper.getXzSqmyjlb(startDate, endDate);
        i.forEach(x->{
            JSONObject jb = new JSONObject();
            jb.put("id", x.get("id"));
            jb.put("shape", x.get("dian"));
            JSONObject jb2 = new JSONObject();
            jb2.put("地址", x.get("qhnxxdz"));
            jb2.put("上报时间", sdf.format(x.get("create_time")));
            jb2.put("事件类型", "民生服务巡查-社情民意巡查");
            jb.put("attribute", jb2);
            data.add(jb);
        });
    }

    private void h(LocalDateTime endDate, LocalDateTime startDate, SimpleDateFormat sdf, ArrayList<Object> data) {
        ArrayList<JSONObject> h = dotDataMapper.getXzKclrxcb(startDate, endDate);
        h.forEach(x->{
            JSONObject jb = new JSONObject();
            jb.put("id", x.get("id"));
            jb.put("shape", x.get("dian"));
            JSONObject jb2 = new JSONObject();
            jb2.put("地址", x.get("xxdz"));
            jb2.put("上报时间", sdf.format(x.get("create_time")));
            jb2.put("事件类型", "民生服务巡查-空巢老人巡查");
            jb.put("attribute", jb2);
            data.add(jb);
        });
    }

    private void g(LocalDateTime endDate, LocalDateTime startDate, SimpleDateFormat sdf, ArrayList<Object> data) {
        ArrayList<JSONObject> g = dotDataMapper.getXzShwdxcjl(startDate, endDate);
        g.forEach(x-> {
                    JSONObject jb = new JSONObject();
                    jb.put("id", x.get("id"));
                    jb.put("shape", x.get("dian"));

                    JSONObject jb2 = new JSONObject();
                    jb2.put("地址", x.get("qhnxxdz"));

                    jb2.put("上报时间", sdf.format(x.get("create_time")));

                    if (x.get("xslxdm").equals("1")) {

                        jb2.put("事件类型", "公共安全巡查-生产隐患上报");

                    } else if (x.get("xslxdm").equals("2")) {

                        jb2.put("事件类型", "社会治安巡查-交通隐患上报");

                    } else if (x.get("xslxdm").equals("3")) {

                        jb2.put("事件类型", "社会稳定巡查-消防隐患上报");

                    } else if (x.get("xslxdm").equals("4")) {

                        jb2.put("事件类型", "社会稳定巡查-食药卫隐患上报");
                    }else if (x.get("xslxdm").equals("5")) {

                        jb2.put("事件类型", "社会稳定巡查-地质隐患上报");
                    }

                    jb.put("attribute", jb2);

                    data.add(jb);
                }
                );
    }

    private void f(LocalDateTime endDate, LocalDateTime startDate, SimpleDateFormat sdf, ArrayList<Object> data) {
        ArrayList<JSONObject> f = dotDataMapper.getXzMdjfjl(startDate, endDate);
        f.forEach(x->{
            JSONObject jb = new JSONObject();
            jb.put("id", x.get("id"));
            jb.put("shape", x.get("dian"));
            JSONObject jb2 = new JSONObject();
            jb2.put("地址", x.get("fsdd_dzmc"));
            jb2.put("上报时间", sdf.format(x.get("create_time")));
            jb2.put("事件类型", "矛盾纠纷巡查-矛盾纠纷");
            jb.put("attribute", jb2);
            data.add(jb);
        });
    }

    private void e(LocalDateTime endDate, LocalDateTime startDate, SimpleDateFormat sdf, ArrayList<Object> data) {
        ArrayList<JSONObject> e = dotDataMapper.getXzSbxsjl(startDate, endDate);
        e.forEach(x->{
            JSONObject jb = new JSONObject();
            jb.put("id", x.get("id"));
            jb.put("shape", x.get("dian"));
            JSONObject jb2 = new JSONObject();
            jb2.put("地址", x.get("qhnxxdz"));
            jb2.put("上报时间", sdf.format(x.get("create_time")));
            jb2.put("事件类型", "社会治安巡查-网络线索上报");
            jb.put("attribute", jb2);
            data.add(jb);
        });
    }

    private void d(LocalDateTime endDate, LocalDateTime startDate, SimpleDateFormat sdf, ArrayList<Object> data) {
        ArrayList<JSONObject> d = dotDataMapper.getXzCzqzfxcjl(startDate, endDate);
        d.forEach(x->{
            JSONObject jb = new JSONObject();
            jb.put("id", x.get("id"));
            jb.put("shape", x.get("dian"));
            JSONObject jb2 = new JSONObject();
            jb2.put("地址", "");
            jb2.put("上报时间", sdf.format(x.get("create_time")));
            jb2.put("事件类型", "社会治安巡查-出租群租房走访");
            jb.put("attribute", jb2);
            data.add(jb);
        });
    }

    private void c(LocalDateTime endDate, LocalDateTime startDate, SimpleDateFormat sdf, ArrayList<Object> data) {
        ArrayList<JSONObject> c = dotDataMapper.getXzZdcsxcjl(startDate, endDate);
        c.forEach(x->{
            JSONObject jb = new JSONObject();
            jb.put("id", x.get("id"));
            jb.put("shape", x.get("dian"));
            JSONObject jb2 = new JSONObject();
            jb2.put("地址", "");
            jb2.put("上报时间", sdf.format(x.get("create_time")));
            jb2.put("事件类型", "社会治安巡查-重点场所巡查");
            jb.put("attribute", jb2);
            data.add(jb);
        });
    }

    private void b(LocalDateTime endDate, LocalDateTime startDate, SimpleDateFormat sdf, ArrayList<Object> data) {
        ArrayList<JSONObject> b = dotDataMapper.getXzZdryxcjl(startDate, endDate);
        b.forEach(x->{
            JSONObject jb = new JSONObject();
            jb.put("id", x.get("id"));
            jb.put("shape", x.get("dian"));
            JSONObject jb2 = new JSONObject();
            jb2.put("地址", x.get("xcd_qhnxxdz"));
            jb2.put("上报时间", sdf.format(x.get("create_time")));
            jb2.put("事件类型", "社会治安巡查-重点人员走访");
            jb.put("attribute", jb2);
            data.add(jb);
        });
    }

    private void a(LocalDateTime endDate, LocalDateTime startDate, SimpleDateFormat sdf, ArrayList<Object> data) {
        ArrayList<JSONObject> a = dotDataMapper.getXzGgaqxcjl(0, startDate, endDate);
        a.forEach(x->{
            JSONObject jb = new JSONObject();
            jb.put("id", x.get("id"));
            jb.put("shape", x.get("dian"));

            JSONObject jb2 = new JSONObject();
            jb2.put("地址", x.get("qhnxxdz"));

            jb2.put("上报时间", sdf.format(x.get("create_time")));

            if (x.get("xslxdm").equals("10")) {

                jb2.put("事件类型", "公共安全巡查-安全问题上报");

            } else if (x.get("xslxdm").equals("20")) {

                jb2.put("事件类型", "社会治安巡查-治安问题上报");

            } else if (x.get("xslxdm").equals("30")) {

                jb2.put("事件类型", "社会稳定巡查-稳定问题上报");

            } else if (x.get("xslxdm").equals("40")) {

                jb2.put("事件类型", "社会稳定巡查-信访问题上报");

            } else if (x.get("xslxdm").equals("50")) {

                jb2.put("事件类型", "公共安全巡查-交通隐患上报");

            } else if (x.get("xslxdm").equals("60")) {

                jb2.put("事件类型", "公共安全巡查-消防隐患上报");

            } else if (x.get("xslxdm").equals("70")) {

                jb2.put("事件类型", "公共安全巡查-装修安全上报");

            }

            jb.put("attribute", jb2);

            data.add(jb);

        });
    }
}
